//测试select * from tb_brand;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class test{
    @Test
    public void  testSelectAll() throws Exception{
        //1.加载mybatis核心配置文件,获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //2.获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.获取BrandMapper代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        List<Brand> brand1 = brandMapper.selectAll();
        System.out.println(brand1);

        sqlSession.close();
    }

    @Test
    public void  testSelectById() throws Exception{
        //1.加载mybatis核心配置文件,获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //2.获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.获取BrandMapper代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        int id = 2;
        Brand brand2 = brandMapper.selectById(id);
        System.out.println(brand2);

        sqlSession.close();
    }

    @Test
    public void  testSelectByCondition() throws Exception{
        //1.加载mybatis核心配置文件,获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //2.获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.获取BrandMapper代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        int status = 1;
        String companyName = "华为";
        String brandName = "华为";
        companyName = "%" + companyName + "%";
        brandName = "%" + brandName + "%";

        Map map = new HashMap();
        map.put("status", status);
        map.put("brandName", brandName);

        List<Brand> brands3 = brandMapper.selectByCondition(map);
        System.out.println(brands3);

        sqlSession.close();
    }

    @Test
    public void  testAdd() throws Exception{
        //1.加载mybatis核心配置文件,获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //2.获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.获取BrandMapper代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        int status = 0;
        String companyName = "苹果2";
        String brandName = "苹果2";

        Map map = new HashMap();
        map.put("status", status);
        map.put("brandName", brandName);
        map.put("companyName", companyName);

        brandMapper.add(map);
        sqlSession.commit();

        sqlSession.close();
    }
}
